package com.imonsoft.sqm.service.excel.AnswerProblemExcel;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.imonsoft.common.constant.ConstantDict;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.common.util.ValidatorUtil;
import com.imonsoft.sqm.pojo.basedata.Dictionary;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.DictionaryService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.web.excel.AnswerProblemExcel.AnswerProblemImportDto;

@Service("answerProblemExcelService")
public class AnswerProblemExcelServiceImpl implements AnswerProblemExcelService{
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private DictionaryService dictionaryService;
	/**
	 * 检查单行数据
	 * @param supplierImportDto
	 * @param setCode
	 * @return
	 */
	@Override
	public List<String> txCheckData(AnswerProblemImportDto answerProblemImportDto) {
		List<String> messages = new ArrayList<String>();
		List<Supplier> supplierList=supplierService.queryAll();
		List<Dictionary> typeList=dictionaryService.findDictionaryByType(ConstantDict.AnswerProblem_ANSER);
		if (supplierList == null || supplierList.size() <= 0) {
			messages.add("系统中无供应商数据，请先维护供应商数据");
		}
		if (typeList == null || typeList.size() <= 0) {
			messages.add("系统中无评价类型数据，请先维护评价类型数据");
		}
		String notGood="";
		String satisfactory="";
		String typeVeryGood="";
		if(typeList!=null&&typeList.size()>0){
			for (Dictionary d:typeList) {
				if(d.getName().equals("NOT GOOD")){
					notGood=d.getCode();
				}
				if(d.getName().equals("SATISFACTORY")){
					satisfactory=d.getCode();
				}
				if(d.getName().equals("GOOD,VERY QUICK")){
					typeVeryGood=d.getCode();
				}
			}
		}
		if (StringUtils.isEmpty(answerProblemImportDto.getSupplierCode())) {
			messages.add("第[" + answerProblemImportDto.getRow() + "]行供应商代码不能为空!");
		} else {
			if(supplierList!=null&&supplierList.size()>0){
				for (Supplier s:supplierList) {
					if(answerProblemImportDto.getSupplierCode().equals(s.getCode())){
						answerProblemImportDto.setSupplierId(Integer.parseInt(s.getId()+""));
						answerProblemImportDto.setSupplierName(s.getName());
					}
				}
			}
			if(answerProblemImportDto.getSupplierId()==null){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行供应商代码不存在!");
			}
		}
		if (StringUtils.isEmpty(answerProblemImportDto.getYear())) {
			messages.add("第[" + answerProblemImportDto.getRow() + "]行年份不能为空!");
		}else if(!ValidatorUtil.isNumber(answerProblemImportDto.getYear())){
			messages.add("第[" + answerProblemImportDto.getRow() + "]行请输入正确的年份!");
		}
		else if(StringUtil.toLong(StringUtil.fixup(answerProblemImportDto.getYear()))<=0){
			messages.add("第[" + answerProblemImportDto.getRow() + "]行请输入正确的年份!");
		}
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueJanuary())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueJanuary())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行1月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueJanuary().equals("0")){
					answerProblemImportDto.setTypeJanuary(notGood);
				}else if(answerProblemImportDto.getValueJanuary().equals("3")){
					answerProblemImportDto.setTypeJanuary(satisfactory);
				}else if(answerProblemImportDto.getValueJanuary().equals("5")){
					answerProblemImportDto.setTypeJanuary(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行1月分没有此分数对应的类型!");
				}
				
			}
		}

		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueFebruary())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueFebruary())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行2月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueFebruary().equals("0")){
					answerProblemImportDto.setTypeFebruary(notGood);
				}else if(answerProblemImportDto.getValueFebruary().equals("3")){
					answerProblemImportDto.setTypeFebruary(satisfactory);
				}else if(answerProblemImportDto.getValueFebruary().equals("5")){
					answerProblemImportDto.setTypeFebruary(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行2月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueMarch())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueMarch())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行3月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueMarch().equals("0")){
					answerProblemImportDto.setTypeMarch(notGood);
				}else if(answerProblemImportDto.getValueMarch().equals("3")){
					answerProblemImportDto.setTypeMarch(satisfactory);
				}else if(answerProblemImportDto.getValueMarch().equals("5")){
					answerProblemImportDto.setTypeMarch(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行3月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueApril())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueApril())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行4月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueApril().equals("0")){
					answerProblemImportDto.setTypeApril(notGood);
				}else if(answerProblemImportDto.getValueApril().equals("3")){
					answerProblemImportDto.setTypeApril(satisfactory);
				}else if(answerProblemImportDto.getValueApril().equals("5")){
					answerProblemImportDto.setTypeApril(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行4月分没有此分数对应的类型!");
				}
				
			}
		}
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueMay())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueMay())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行5月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueMay().equals("0")){
					answerProblemImportDto.setTypeMay(notGood);
				}else if(answerProblemImportDto.getValueMay().equals("3")){
					answerProblemImportDto.setTypeMay(satisfactory);
				}else if(answerProblemImportDto.getValueMay().equals("5")){
					answerProblemImportDto.setTypeMay(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行5月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueJune())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueJune())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行6月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueJune().equals("0")){
					answerProblemImportDto.setTypeJune(notGood);
				}else if(answerProblemImportDto.getValueJune().equals("3")){
					answerProblemImportDto.setTypeJune(satisfactory);
				}else if(answerProblemImportDto.getValueJune().equals("5")){
					answerProblemImportDto.setTypeJune(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行6月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueJuly())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueJuly())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行7月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueJuly().equals("0")){
					answerProblemImportDto.setTypeJuly(notGood);
				}else if(answerProblemImportDto.getValueJuly().equals("3")){
					answerProblemImportDto.setTypeJuly(satisfactory);
				}else if(answerProblemImportDto.getValueJuly().equals("5")){
					answerProblemImportDto.setTypeJuly(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行7月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueAugust())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueAugust())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行8月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueAugust().equals("0")){
					answerProblemImportDto.setTypeAugust(notGood);
				}else if(answerProblemImportDto.getValueAugust().equals("3")){
					answerProblemImportDto.setTypeAugust(satisfactory);
				}else if(answerProblemImportDto.getValueAugust().equals("5")){
					answerProblemImportDto.setTypeAugust(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行8月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueSeptember())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueSeptember())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行9月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueSeptember().equals("0")){
					answerProblemImportDto.setTypeSeptember(notGood);
				}else if(answerProblemImportDto.getValueSeptember().equals("3")){
					answerProblemImportDto.setTypeSeptember(satisfactory);
				}else if(answerProblemImportDto.getValueSeptember().equals("5")){
					answerProblemImportDto.setTypeSeptember(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行9月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueOctober())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueOctober())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行10月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueOctober().equals("0")){
					answerProblemImportDto.setTypeOctober(notGood);
				}else if(answerProblemImportDto.getValueOctober().equals("3")){
					answerProblemImportDto.setTypeOctober(satisfactory);
				}else if(answerProblemImportDto.getValueOctober().equals("5")){
					answerProblemImportDto.setTypeOctober(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行10月分没有此分数对应的类型!");
				}
				
			}
		}
		
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueNovember())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueNovember())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行11月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueNovember().equals("0")){
					answerProblemImportDto.setTypeNovember(notGood);
				}else if(answerProblemImportDto.getValueNovember().equals("3")){
					answerProblemImportDto.setTypeNovember(satisfactory);
				}else if(answerProblemImportDto.getValueNovember().equals("5")){
					answerProblemImportDto.setTypeNovember(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行11月分没有此分数对应的类型!");
				}
				
			}
		}
		
		if(StringUtil.isNotEmpty(answerProblemImportDto.getValueDecember())){
			if(!ValidatorUtil.isNumber(answerProblemImportDto.getValueDecember())){
				messages.add("第[" + answerProblemImportDto.getRow() + "]行12月分数请输入数值!");
			}else{
				if(answerProblemImportDto.getValueDecember().equals("0")){
					answerProblemImportDto.setTypeDecember(notGood);
				}else if(answerProblemImportDto.getValueDecember().equals("3")){
					answerProblemImportDto.setTypeDecember(satisfactory);
				}else if(answerProblemImportDto.getValueDecember().equals("5")){
					answerProblemImportDto.setTypeDecember(typeVeryGood);
				}else{
					messages.add("第[" + answerProblemImportDto.getRow() + "]行12月分没有此分数对应的类型!");
				}
				
			}
		}
		
		return messages;
	}
}
